<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>firststrong.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_editor_plugins_firststrong.js.html">firststrong.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A plugin to enable the First Strong Bidi algorithm.  The First
<a name="line17"></a> * Strong algorithm as a heuristic used to automatically set paragraph direction
<a name="line18"></a> * depending on its content.
<a name="line19"></a> *
<a name="line20"></a> * In the documentation below, a &#39;paragraph&#39; is the local element which we
<a name="line21"></a> * evaluate as a whole for purposes of determining directionality. It may be a
<a name="line22"></a> * block-level element (e.g. &amp;lt;div&amp;gt;) or a whole list (e.g. &amp;lt;ul&amp;gt;).
<a name="line23"></a> *
<a name="line24"></a> * This implementation is based on, but is not identical to, the original
<a name="line25"></a> * First Strong algorithm defined in Unicode
<a name="line26"></a> * @see http://www.unicode.org/reports/tr9/
<a name="line27"></a> * The central difference from the original First Strong algorithm is that this
<a name="line28"></a> * implementation decides the paragraph direction based on the first strong
<a name="line29"></a> * character that is &lt;em&gt;typed&lt;/em&gt; into the paragraph, regardless of its
<a name="line30"></a> * location in the paragraph, as opposed to the original algorithm where it is
<a name="line31"></a> * the first character in the paragraph &lt;em&gt;by location&lt;/em&gt;, regardless of
<a name="line32"></a> * whether other strong characters already appear in the paragraph, further its
<a name="line33"></a> * start.
<a name="line34"></a> *
<a name="line35"></a> * &lt;em&gt;Please note&lt;/em&gt; that this plugin does not perform the direction change
<a name="line36"></a> * itself. Rather, it fires editor commands upon the key up event when a
<a name="line37"></a> * direction change needs to be performed; {@code goog.editor.Command.DIR_RTL}
<a name="line38"></a> * or {@code goog.editor.Command.DIR_RTL}.
<a name="line39"></a> *
<a name="line40"></a> */
<a name="line41"></a>
<a name="line42"></a>goog.provide(&#39;goog.editor.plugins.FirstStrong&#39;);
<a name="line43"></a>
<a name="line44"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line45"></a>goog.require(&#39;goog.dom.TagIterator&#39;);
<a name="line46"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line47"></a>goog.require(&#39;goog.editor.Command&#39;);
<a name="line48"></a>goog.require(&#39;goog.editor.Plugin&#39;);
<a name="line49"></a>goog.require(&#39;goog.editor.node&#39;);
<a name="line50"></a>goog.require(&#39;goog.editor.range&#39;);
<a name="line51"></a>goog.require(&#39;goog.i18n.bidi&#39;);
<a name="line52"></a>goog.require(&#39;goog.i18n.uChar&#39;);
<a name="line53"></a>goog.require(&#39;goog.iter&#39;);
<a name="line54"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>/**
<a name="line59"></a> * First Strong plugin.
<a name="line60"></a> * @constructor
<a name="line61"></a> * @extends {goog.editor.Plugin}
<a name="line62"></a> * @final
<a name="line63"></a> */
<a name="line64"></a>goog.editor.plugins.FirstStrong = function() {
<a name="line65"></a>  goog.editor.plugins.FirstStrong.base(this, &#39;constructor&#39;);
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * Indicates whether or not the cursor is in a paragraph we have not yet
<a name="line69"></a>   * finished evaluating for directionality. This is set to true whenever the
<a name="line70"></a>   * cursor is moved, and set to false after seeing a strong character in the
<a name="line71"></a>   * paragraph the cursor is currently in.
<a name="line72"></a>   *
<a name="line73"></a>   * @type {boolean}
<a name="line74"></a>   * @private
<a name="line75"></a>   */
<a name="line76"></a>  this.isNewBlock_ = true;
<a name="line77"></a>
<a name="line78"></a>  /**
<a name="line79"></a>   * Indicates whether or not the current paragraph the cursor is in should be
<a name="line80"></a>   * set to Right-To-Left directionality.
<a name="line81"></a>   *
<a name="line82"></a>   * @type {boolean}
<a name="line83"></a>   * @private
<a name="line84"></a>   */
<a name="line85"></a>  this.switchToRtl_ = false;
<a name="line86"></a>
<a name="line87"></a>  /**
<a name="line88"></a>   * Indicates whether or not the current paragraph the cursor is in should be
<a name="line89"></a>   * set to Left-To-Right directionality.
<a name="line90"></a>   *
<a name="line91"></a>   * @type {boolean}
<a name="line92"></a>   * @private
<a name="line93"></a>   */
<a name="line94"></a>  this.switchToLtr_ = false;
<a name="line95"></a>};
<a name="line96"></a>goog.inherits(goog.editor.plugins.FirstStrong, goog.editor.Plugin);
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/** @override */
<a name="line100"></a>goog.editor.plugins.FirstStrong.prototype.getTrogClassId = function() {
<a name="line101"></a>  return &#39;FirstStrong&#39;;
<a name="line102"></a>};
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/** @override */
<a name="line106"></a>goog.editor.plugins.FirstStrong.prototype.queryCommandValue =
<a name="line107"></a>    function(command) {
<a name="line108"></a>  return false;
<a name="line109"></a>};
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/** @override */
<a name="line113"></a>goog.editor.plugins.FirstStrong.prototype.handleSelectionChange =
<a name="line114"></a>    function(e, node) {
<a name="line115"></a>  this.isNewBlock_ = true;
<a name="line116"></a>  return false;
<a name="line117"></a>};
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * The name of the attribute which records the input text.
<a name="line122"></a> *
<a name="line123"></a> * @type {string}
<a name="line124"></a> * @const
<a name="line125"></a> */
<a name="line126"></a>goog.editor.plugins.FirstStrong.INPUT_ATTRIBUTE = &#39;fs-input&#39;;
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/** @override */
<a name="line130"></a>goog.editor.plugins.FirstStrong.prototype.handleKeyPress = function(e) {
<a name="line131"></a>  if (!this.isNewBlock_) {
<a name="line132"></a>    return false;  // We&#39;ve already determined this paragraph&#39;s direction.
<a name="line133"></a>  }
<a name="line134"></a>  // Ignore non-character key press events.
<a name="line135"></a>  if (e.ctrlKey || e.metaKey) {
<a name="line136"></a>    return false;
<a name="line137"></a>  }
<a name="line138"></a>  var newInput = goog.i18n.uChar.fromCharCode(e.charCode);
<a name="line139"></a>
<a name="line140"></a>  // IME&#39;s may return 0 for the charCode, which is a legitimate, non-Strong
<a name="line141"></a>  // charCode, or they may return an illegal charCode (for which newInput will
<a name="line142"></a>  // be false).
<a name="line143"></a>  if (!newInput || !e.charCode) {
<a name="line144"></a>    var browserEvent = e.getBrowserEvent();
<a name="line145"></a>    if (browserEvent) {
<a name="line146"></a>      if (goog.userAgent.IE &amp;&amp; browserEvent[&#39;getAttribute&#39;]) {
<a name="line147"></a>        newInput = browserEvent[&#39;getAttribute&#39;](
<a name="line148"></a>            goog.editor.plugins.FirstStrong.INPUT_ATTRIBUTE);
<a name="line149"></a>      } else {
<a name="line150"></a>        newInput = browserEvent[
<a name="line151"></a>            goog.editor.plugins.FirstStrong.INPUT_ATTRIBUTE];
<a name="line152"></a>      }
<a name="line153"></a>    }
<a name="line154"></a>  }
<a name="line155"></a>
<a name="line156"></a>  if (!newInput) {
<a name="line157"></a>    return false;  // Unrecognized key.
<a name="line158"></a>  }
<a name="line159"></a>
<a name="line160"></a>  var isLtr = goog.i18n.bidi.isLtrChar(newInput);
<a name="line161"></a>  var isRtl = !isLtr &amp;&amp; goog.i18n.bidi.isRtlChar(newInput);
<a name="line162"></a>  if (!isLtr &amp;&amp; !isRtl) {
<a name="line163"></a>    return false;  // This character cannot change anything (it is not Strong).
<a name="line164"></a>  }
<a name="line165"></a>  // This character is Strongly LTR or Strongly RTL. We might switch direction
<a name="line166"></a>  // on it now, but in any case we do not need to check any more characters in
<a name="line167"></a>  // this paragraph after it.
<a name="line168"></a>  this.isNewBlock_ = false;
<a name="line169"></a>
<a name="line170"></a>  // Are there no Strong characters already in the paragraph?
<a name="line171"></a>  if (this.isNeutralBlock_()) {
<a name="line172"></a>    this.switchToRtl_ = isRtl;
<a name="line173"></a>    this.switchToLtr_ = isLtr;
<a name="line174"></a>  }
<a name="line175"></a>  return false;
<a name="line176"></a>};
<a name="line177"></a>
<a name="line178"></a>
<a name="line179"></a>/**
<a name="line180"></a> * Calls the flip directionality commands.  This is done here so things go into
<a name="line181"></a> * the redo-undo stack at the expected order; fist enter the input, then flip
<a name="line182"></a> * directionality.
<a name="line183"></a> * @override
<a name="line184"></a> */
<a name="line185"></a>goog.editor.plugins.FirstStrong.prototype.handleKeyUp = function(e) {
<a name="line186"></a>  if (this.switchToRtl_) {
<a name="line187"></a>    var field = this.getFieldObject();
<a name="line188"></a>    field.dispatchChange(true);
<a name="line189"></a>    field.execCommand(goog.editor.Command.DIR_RTL);
<a name="line190"></a>    this.switchToRtl_ = false;
<a name="line191"></a>  } else if (this.switchToLtr_) {
<a name="line192"></a>    var field = this.getFieldObject();
<a name="line193"></a>    field.dispatchChange(true);
<a name="line194"></a>    field.execCommand(goog.editor.Command.DIR_LTR);
<a name="line195"></a>    this.switchToLtr_ = false;
<a name="line196"></a>  }
<a name="line197"></a>  return false;
<a name="line198"></a>};
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>/**
<a name="line202"></a> * @return {Element} The lowest Block element ancestor of the node where the
<a name="line203"></a> *     next character will be placed.
<a name="line204"></a> * @private
<a name="line205"></a> */
<a name="line206"></a>goog.editor.plugins.FirstStrong.prototype.getBlockAncestor_ = function() {
<a name="line207"></a>  var start = this.getFieldObject().getRange().getStartNode();
<a name="line208"></a>  // Go up in the DOM until we reach a Block element.
<a name="line209"></a>  while (!goog.editor.plugins.FirstStrong.isBlock_(start)) {
<a name="line210"></a>    start = start.parentNode;
<a name="line211"></a>  }
<a name="line212"></a>  return /** @type {Element} */ (start);
<a name="line213"></a>};
<a name="line214"></a>
<a name="line215"></a>
<a name="line216"></a>/**
<a name="line217"></a> * @return {boolean} Whether the paragraph where the next character will be
<a name="line218"></a> *     entered contains only non-Strong characters.
<a name="line219"></a> * @private
<a name="line220"></a> */
<a name="line221"></a>goog.editor.plugins.FirstStrong.prototype.isNeutralBlock_ = function() {
<a name="line222"></a>  var root = this.getBlockAncestor_();
<a name="line223"></a>  // The exact node with the cursor location. Simply calling getStartNode() on
<a name="line224"></a>  // the range only returns the containing block node.
<a name="line225"></a>  var cursor = goog.editor.range.getDeepEndPoint(
<a name="line226"></a>      this.getFieldObject().getRange(), false).node;
<a name="line227"></a>
<a name="line228"></a>  // In FireFox the BR tag also represents a change in paragraph if not inside a
<a name="line229"></a>  // list. So we need special handling to only look at the sub-block between
<a name="line230"></a>  // BR elements.
<a name="line231"></a>  var blockFunction = (goog.userAgent.GECKO &amp;&amp;
<a name="line232"></a>      !this.isList_(root)) ?
<a name="line233"></a>          goog.editor.plugins.FirstStrong.isGeckoBlock_ :
<a name="line234"></a>          goog.editor.plugins.FirstStrong.isBlock_;
<a name="line235"></a>  var paragraph = this.getTextAround_(root, cursor,
<a name="line236"></a>      blockFunction);
<a name="line237"></a>  // Not using {@code goog.i18n.bidi.isNeutralText} as it contains additional,
<a name="line238"></a>  // unwanted checks to the content.
<a name="line239"></a>  return !goog.i18n.bidi.hasAnyLtr(paragraph) &amp;&amp;
<a name="line240"></a>      !goog.i18n.bidi.hasAnyRtl(paragraph);
<a name="line241"></a>};
<a name="line242"></a>
<a name="line243"></a>
<a name="line244"></a>/**
<a name="line245"></a> * Checks if an element is a list element (&#39;UL&#39; or &#39;OL&#39;).
<a name="line246"></a> *
<a name="line247"></a> * @param {Element} element The element to test.
<a name="line248"></a> * @return {boolean} Whether the element is a list element (&#39;UL&#39; or &#39;OL&#39;).
<a name="line249"></a> * @private
<a name="line250"></a> */
<a name="line251"></a>goog.editor.plugins.FirstStrong.prototype.isList_ = function(element) {
<a name="line252"></a>  if (!element) {
<a name="line253"></a>    return false;
<a name="line254"></a>  }
<a name="line255"></a>  var tagName = element.tagName;
<a name="line256"></a>  return tagName == goog.dom.TagName.UL || tagName == goog.dom.TagName.OL;
<a name="line257"></a>};
<a name="line258"></a>
<a name="line259"></a>
<a name="line260"></a>/**
<a name="line261"></a> * Returns the text within the local paragraph around the cursor.
<a name="line262"></a> * Notice that for GECKO a BR represents a pargraph change despite not being a
<a name="line263"></a> * block element.
<a name="line264"></a> *
<a name="line265"></a> * @param {Element} root The first block element ancestor of the node the cursor
<a name="line266"></a> *     is in.
<a name="line267"></a> * @param {Node} cursorLocation Node where the cursor currently is, marking the
<a name="line268"></a> *     paragraph whose text we will return.
<a name="line269"></a> * @param {function(Node): boolean} isParagraphBoundary The function to
<a name="line270"></a> *     determine if a node represents the start or end of the paragraph.
<a name="line271"></a> * @return {string} the text in the paragraph around the cursor location.
<a name="line272"></a> * @private
<a name="line273"></a> */
<a name="line274"></a>goog.editor.plugins.FirstStrong.prototype.getTextAround_ = function(root,
<a name="line275"></a>    cursorLocation, isParagraphBoundary) {
<a name="line276"></a>  // The buffer where we&#39;re collecting the text.
<a name="line277"></a>  var buffer = [];
<a name="line278"></a>  // Have we reached the cursor yet, or are we still before it?
<a name="line279"></a>  var pastCursorLocation = false;
<a name="line280"></a>
<a name="line281"></a>  if (root &amp;&amp; cursorLocation) {
<a name="line282"></a>    goog.iter.some(new goog.dom.TagIterator(root), function(node) {
<a name="line283"></a>      if (node == cursorLocation) {
<a name="line284"></a>        pastCursorLocation = true;
<a name="line285"></a>      } else if (isParagraphBoundary(node)) {
<a name="line286"></a>        if (pastCursorLocation) {
<a name="line287"></a>          // This is the end of the paragraph containing the cursor. We&#39;re done.
<a name="line288"></a>          return true;
<a name="line289"></a>        } else {
<a name="line290"></a>          // All we collected so far does not count; it was in a previous
<a name="line291"></a>          // paragraph that did not contain the cursor.
<a name="line292"></a>          buffer = [];
<a name="line293"></a>        }
<a name="line294"></a>      }
<a name="line295"></a>      if (node.nodeType == goog.dom.NodeType.TEXT) {
<a name="line296"></a>        buffer.push(node.nodeValue);
<a name="line297"></a>      }
<a name="line298"></a>      return false;  // Keep going.
<a name="line299"></a>    });
<a name="line300"></a>  }
<a name="line301"></a>  return buffer.join(&#39;&#39;);
<a name="line302"></a>};
<a name="line303"></a>
<a name="line304"></a>
<a name="line305"></a>/**
<a name="line306"></a> * @param {Node} node Node to check.
<a name="line307"></a> * @return {boolean} Does the given node represent a Block element? Notice we do
<a name="line308"></a> *     not consider list items as Block elements in the algorithm.
<a name="line309"></a> * @private
<a name="line310"></a> */
<a name="line311"></a>goog.editor.plugins.FirstStrong.isBlock_ = function(node) {
<a name="line312"></a>  return !!node &amp;&amp; goog.editor.node.isBlockTag(node) &amp;&amp;
<a name="line313"></a>      node.tagName != goog.dom.TagName.LI;
<a name="line314"></a>};
<a name="line315"></a>
<a name="line316"></a>
<a name="line317"></a>/**
<a name="line318"></a> * @param {Node} node Node to check.
<a name="line319"></a> * @return {boolean} Does the given node represent a Block element from the
<a name="line320"></a> *     point of view of FireFox? Notice we do not consider list items as Block
<a name="line321"></a> *     elements in the algorithm.
<a name="line322"></a> * @private
<a name="line323"></a> */
<a name="line324"></a>goog.editor.plugins.FirstStrong.isGeckoBlock_ = function(node) {
<a name="line325"></a>  return !!node &amp;&amp; (node.tagName == goog.dom.TagName.BR ||
<a name="line326"></a>      goog.editor.plugins.FirstStrong.isBlock_(node));
<a name="line327"></a>};
</pre>


</body>
</html>
